<template>
  <div class="firstdemo" >
    <el-form ref="form" :model="form"  :xs="8" :sm="6" :md="4" :lg="3" :xl="1">
      <el-row type="flex" justify="center">
        <el-col :span="5">
          <el-form-item label="用户名">
            <el-input v-model="form.name"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row type="flex" justify="center">
        <el-col :span="5">
          <el-form-item label="密码">
            <el-input type="password" v-model="form.password"></el-input>
          </el-form-item>
        </el-col>
      </el-row>

      <el-row type="flex" justify="center">
        <el-col :span="5">
          <el-form-item>
            <el-button type="primary" @click="onSubmit">登录</el-button>
         
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
  </div>
</template>
<script>
import post from '@/post'
import store from '@/store'
import router from '@/router'
export default {
  name: "fisrtdemo",
  data() {
    return {
      form: {
        name: "",
        password: ""
      }
    };
  },
  mounted(){
    if(store.state.userinfo.token){
      router.push("/")
    }
  },
  methods: {
    onSubmit() {
      let data = {
        userid:this.form.name,
        pwd:this.form.password
      }
      post("/login",data).then(d=>{
        if(d['ok']==1 ){
          let info = {
            token : d.token,
            // expiretime : d.exp,
            logintime : d.logintime,
            userinfo : {
              name : this.form.name,
            }
          }
          store.commit("setUserInfo",info)
          this.$router.push("/")
        }else{
          this.$message({message: d['err']||"登录失败",type:"error"})
        }
      }).catch(e=>{
        console.error(e)
        this.$message({message:"登录失败.err",type:"error"})
      })
    }
  }
};
</script>
<style lang="stylus" scoped></style>
